﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Hematology.Data;
using Hematology.Tools;
using Hematology.UserSite.Controls;
using NgoResource = Hematology.UserSite.App_GlobalResources.UserSite;

namespace Hematology.UserSite.admin
{
	public partial class Hospitals : NgoPage
	{
		#region Fields

		private const string ListUrl = "/admin/Hospitals.aspx?view=List";

		#endregion

		#region Properties

		private EnumHelper.PageSection CurrentPageSection
		{
			get
			{
				return ViewState["CurrentPageSection"] != null
						   ? (EnumHelper.PageSection)Enum.Parse(typeof(EnumHelper.PageSection), ViewState["CurrentPageSection"].ToString())
						   : EnumHelper.PageSection.List;
			}
			set { ViewState["CurrentPageSection"] = value; }
		}

		private Job CurrentJob
		{
			get { return (Job)ViewState["CurrentJob"]; }
			set { ViewState["CurrentJob"] = value; }
		}

		protected int CountryId
		{
			get { return Convert.ToInt32(ViewState["CountryId"]); }
			set { ViewState["CountryId"] = value; }
		}

		protected int RegionId
		{
			get { return Convert.ToInt32(ViewState["RegionId"]); }
			set { ViewState["RegionId"] = value; }
		}

		protected int CityId
		{
			get { return Convert.ToInt32(ViewState["CityId"]); }
			set { ViewState["CityId"] = value; }
		}

		protected DropDownList DdlCountry
		{
			get { return (DropDownList)mvHospitals.FindControl(DdlCountryName); }
		}

		protected DropDownList DdlRegion
		{
			get { return (DropDownList)mvHospitals.FindControl(DdlRegionName); }
		}

		protected DropDownList DdlCity
		{
			get { return (DropDownList)mvHospitals.FindControl(DdlCityName); }
		}

		protected string DdlCountryName
		{
			get { return ViewState["DdlCountryName"].ToString(); }
			set { ViewState["DdlCountryName"] = value; }
		}

		protected string DdlRegionName
		{
			get { return ViewState["DdlRegionName"].ToString(); }
			set { ViewState["DdlRegionName"] = value; }
		}

		protected string DdlCityName
		{
			get { return ViewState["DdlCityName"].ToString(); }
			set { ViewState["DdlCityName"] = value; }
		}

		#endregion

		private void FillViewStateVariables()
		{
			string pageSection = Request.QueryString["view"];
			if (Array.IndexOf(Enum.GetNames(typeof(EnumHelper.PageSection)), pageSection) > -1)
			{
				CurrentPageSection =
					(EnumHelper.PageSection)Enum.Parse(typeof(EnumHelper.PageSection), pageSection);
			}
		}

		private void FillEditForm()
		{
			string sJobId = Request.QueryString["id"];
			int iJobId;
			if (!String.IsNullOrEmpty(sJobId) && Int32.TryParse(sJobId, out iJobId))
			{
				Job jobItem = Job.Get(iJobId);
				if (jobItem != null)
				{
					CurrentJob = jobItem;

					txtHospitalFullName.Text = jobItem.FullName;
					txtHospitalAbbreviation.Text = jobItem.Abbreviation;
				}
			}
		}

		private void FillPage()
		{
			switch (CurrentPageSection)
			{
				case EnumHelper.PageSection.Edit:
				case EnumHelper.PageSection.Add:
					mvHospitals.ActiveViewIndex = 1;

					DdlCountryName = ddlCountryEdit.ID;
					DdlRegionName = ddlRegionEdit.ID;
					DdlCityName = ddlCityEdit.ID;

					FillEditForm();

					break;
				default:
					mvHospitals.ActiveViewIndex = 0;

					DdlCountryName = ddlCountryView.ID;
					DdlRegionName = ddlRegionView.ID;
					DdlCityName = ddlCityView.ID;

					break;
			}
			FillCountryCombo();
			FillRegionCombo(0, 0);
			FillCityCombo(0, 0);
		}

		private void FillCountryCombo()
		{
			List<Country> lCountries = new List<Country>
			                           	{
			                           		new Country
			                           			{
			                           				Id = 0,
			                           				Name = NgoResource.resSelectCountryCombo
			                           			}
			                           	};

			lCountries.AddRange(Country.GetAllCountries(true));
			DdlCountry.DataSource = lCountries;
			DdlCountry.DataBind();

			if (CountryId != 0)
			{
				DdlCountry.SelectedValue = CountryId.ToString();
			}
		}

		private void FillRegionCombo(int countryId, int? cityId)
		{
			List<Region> lRegion = new List<Region>
                                           {
                                               new Region
                                                   {
                                                       Id = 0,
                                                       Name = NgoResource.resSelectRegionCombo
                                                   }
                                           };
			if (countryId > 0)
			{
				lRegion.AddRange(Region.GetAllRegions(countryId, true));
			}
			DdlRegion.DataSource = lRegion;
			DdlRegion.DataBind();

			if (RegionId > 0)
			{
				DdlRegion.SelectedValue = RegionId.ToString();
			}
			else if (cityId > 0)
			{
				int? regionId = City.Get(cityId.Value).RegionId;
				if (regionId != null)
					DdlRegion.SelectedValue = regionId.Value.ToString();
			}
		}

		private void FillCityCombo(int countryId, int? regionId)
		{
			List<City> lCity = new List<City>
                                           {
                                               new City
                                                   {
                                                       Id = 0,
                                                       Name = NgoResource.resSelectCityCombo
                                                   }
                                           };
			if (countryId > 0)
			{
				lCity.AddRange(City.GetAllCities(countryId, regionId, true));
			}
			DdlCity.DataSource = lCity;
			DdlCity.DataBind();

			if (CityId != 0)
			{
				DdlCity.SelectedValue = CityId.ToString();
			}
		}

		private void FillJobList(int countryId, int? regionId, int? cityId)
		{
			if (!String.IsNullOrEmpty(DdlCountry.SelectedValue))
			{
				List<Job> lJob = Job.GetAllJobs(countryId, regionId, cityId);
				rptHospital.DataSource = lJob;
				rptHospital.DataBind();
			}
		}

		protected void ddlCountry_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!String.IsNullOrEmpty(DdlCountry.SelectedValue))
			{
				int iCountryId = Convert.ToInt32(DdlCountry.SelectedItem.Value);
				FillRegionCombo(iCountryId, null);
				FillCityCombo(iCountryId, null);
				FillJobList(iCountryId, null, null);
			}
		}

		protected void ddlRegion_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!String.IsNullOrEmpty(DdlRegion.SelectedValue))
			{
				int iCountryId = Convert.ToInt32(DdlCountry.SelectedItem.Value);
				int iRegionId = Convert.ToInt32(DdlRegion.SelectedValue);
				FillCityCombo(iCountryId, iRegionId);
				FillJobList(iCountryId, iRegionId, null);
			}
		}

		protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (!String.IsNullOrEmpty(DdlCity.SelectedValue))
			{
				int iCountryId = Convert.ToInt32(DdlCountry.SelectedItem.Value);
				int iRegionId = Convert.ToInt32(DdlRegion.SelectedValue);
				int iCityId = Convert.ToInt32(DdlCity.SelectedValue);
				FillRegionCombo(iCountryId, iCityId);
				FillJobList(iCountryId, iRegionId, iCityId);
			}
		}

		protected void btnSave_Click(object sender, EventArgs e)
		{
			CountryId = Convert.ToInt32(DdlCountry.SelectedValue);
			RegionId = Convert.ToInt32(DdlRegion.SelectedValue);
			CityId = Convert.ToInt32(DdlCity.SelectedValue);

			if (CurrentPageSection == EnumHelper.PageSection.Add)
			{
				Job newJobItem = new Job
				{
					FullName = txtHospitalFullName.Text.Trim(),
					Abbreviation = txtHospitalAbbreviation.Text.Trim(),
					CountryId = CountryId,
					RegionId = RegionId,
					CityId = CityId
				};

				newJobItem.Save();

				if (newJobItem.JobId != null)
					Response.Redirect(ListUrl);
			}

			if (CurrentPageSection == EnumHelper.PageSection.Edit)
			{
				CurrentJob.FullName = txtHospitalFullName.Text.Trim();
				CurrentJob.Abbreviation = txtHospitalAbbreviation.Text.Trim();
				CurrentJob.CountryId = CountryId;
				CurrentJob.RegionId = RegionId;
				CurrentJob.CityId = CityId;

				CurrentJob.UpdateInstance();

				Response.Redirect(ListUrl);
			}
		}

		protected void btnList_Click(object sender, EventArgs e)
		{
			Response.Redirect(ListUrl);
		}

		protected void Page_Load(object sender, EventArgs e)
		{
			if (Page.IsPostBack)
				return;

			FillViewStateVariables();
			FillPage();
		}

		protected void rptHospital_ItemCommand(object source, RepeaterCommandEventArgs e)
		{
			if (e.CommandName == "DeleteJobItem")
			{
				int iJobItemId = Convert.ToInt32(e.CommandArgument);
				Job jobItem = Job.Get(iJobItemId);
				jobItem.MarkAsDeleted();
			}
		}
	}
}